package com.yocard.yxyd.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.widget.TextView;

import com.yocard.yxyd.R;
import com.yocard.yxyd.http.model.response.InviteRecord;
import com.yocard.yxyd.utils.util.TimeUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created on 2019/5/30
 * 邀请记录
 */
public class InviteRecordsAdapter extends RecyclerView.Adapter<InviteRecordsAdapter.ViewHolder> {
    public static final String FINISHED = "1";

    private final int TYPE_TOP = 0x11;
    private final int TYPE_NORMOL = 0x22;
    private final int TYPE_BOTTOM = 0x33;
    private List<InviteRecord.DataBean> list;
    private String type; //区分已挖成(finished)和未完成

    public InviteRecordsAdapter(List<InviteRecord.DataBean> list) {
        this.list = list;
    }

    public void setType(String type) {
        this.type = type;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_invite_record, viewGroup, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        InviteRecord.DataBean dataBean = list.get(i);
        viewHolder.tvCount.setText(String.valueOf(dataBean.getCoin()));
        viewHolder.tvAccount.setText(dataBean.getMobile());
        viewHolder.tvTime.setText("申请时间：" + TimeUtils.millis2String(dataBean.getCreated_at() * 1000, TimeUtils.DATE_STYLE1));

        if (FINISHED.equals(type)) {
            viewHolder.tvCountUnit.setTextColor(viewHolder.tvCountUnit.getContext().getResources().getColor(R.color.color_9f9f9f));
            viewHolder.tvCount.setVisibility(View.VISIBLE);
            viewHolder.tvCountUnit.setText(R.string.gold);
            return;
        }
        viewHolder.tvCountUnit.setTextColor(viewHolder.tvCountUnit.getContext().getResources().getColor(R.color.color_ff001f));
        viewHolder.tvCountUnit.setText(R.string.unauth);
        viewHolder.tvCount.setVisibility(View.GONE);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TOP;
        } else if (position == list.size() - 1) {
            return TYPE_BOTTOM;
        } else {
            return TYPE_NORMOL;
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_account)
        TextView tvAccount;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_count_unit)
        TextView tvCountUnit;
        @BindView(R.id.tv_count)
        TextView tvCount;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (callback != null) {
                String id = (String) v.getTag();
                callback.onReceiveValue(id);
            }
        }
    };

    private ValueCallback<String> callback;

    public void setOnItemClickListener(ValueCallback<String> callback) {
        this.callback = callback;
    }


}
